﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NGUIDebugLabel : MonoBehaviour {
    private string m_sContent;

    public string Content
    {
        get { return m_sContent; }
        set
        {
            m_sContent = value;
            FreshContent(value);
        }
    }

    private UILabel m_pLabel;
    private UIFollowTarget m_pFollow;
    public Transform target;

    public static Transform RootNode;
    public static UnityEngine.Font ArialFont;


    private void Awake()
    {
        if(RootNode == null)
        {
            if (UIRoot.list.Count > 0)
            {
                RootNode = NGUITools.AddWidget<UIWidget>(UIRoot.list[0].gameObject).transform;
                RootNode.name = "Root_DebugLab";
            }
        }

        if(ArialFont == null)
        {
            UnityEngine.Object[] fonts = Resources.FindObjectsOfTypeAll(typeof(UnityEngine.Font));
            for (int i = 0; i < fonts.Length; i++)
            {
                if (fonts[i].name.Equals("Arial"))
                {
                    ArialFont = fonts[i] as UnityEngine.Font;
                }
            }
        }

        if(RootNode != null)
        {
            m_pLabel = NGUITools.AddChild<UILabel>(RootNode.gameObject);
            m_pLabel.trueTypeFont = ArialFont;
        }
    }

    private void FreshContent(string content)
    {
        if (m_pLabel != null)
        {
            m_pLabel.text = content;
        }
    }

    public void SetTage(Transform tag)
    {
        this.target = tag;
        if (m_pFollow == null)
        {
            m_pFollow = m_pLabel.gameObject.AddComponent<UIFollowTarget>();
        }
        m_pFollow.target = tag;
        m_pLabel.name = "Tagert: "+tag.name;
    }
}
